package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.service.AdminAuthService;
import com.cskaoyan.market.util.MybatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName AdminAuthServiceImpl
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/6/19 10:57
 * @Version V1.0
 **/
public class AdminAuthServiceImpl implements AdminAuthService {
    @Override
    public MarketAdmin login(String username, String password) {
        //调用数据层----mybatis
        InputStream inputStream = null;
        MarketAdmin marketAdmin = null;

            // [com.cskaoyan.market.controller.AdminAuthController] are both mapped to the url-pattern [/admin/auth/*] which is not permitted
//            //根据类的作用域，对这些方法进行改造
//            inputStream = Resources.getResourceAsStream("mybatis.xml");
//            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = MybatisUtil.getSession();
//            SqlSession sqlSession = factory.openSession();
            MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
            //Example类主要用来去构建查询条件
            MarketAdminExample marketAdminExample = new MarketAdminExample();
            //创建条件语句、构建具体的条件
            MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
            criteria.andUsernameEqualTo(username).andPasswordEqualTo(password);
            marketAdmin = adminMapper.selectOneByExample(marketAdminExample);
            sqlSession.commit();
            sqlSession.close();
        return marketAdmin;
    }
}
